package system

import (
	"encoding/json"
	"ltbz/cultural_tourism_api/controllers"
	"ltbz/cultural_tourism_api/models"
	"ltbz/cultural_tourism_api/models/system"
	"time"
)

// 系统管理
type SysConfigController struct {
	controllers.BaseAuthController
}

// @Title 获取所有配置
// @Description 获取所有配置
// @Success 200 {object} system.SysConfigAllResp
// @router /config/all [post]
func (this *SysConfigController) ConfigAll() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sysConfigObj := new(system.SysConfig)
	list, err := sysConfigObj.GetSysConfigAll()
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	resp := new(system.SysConfigAllResp)
	resp.List = list

	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = resp
	return
}

// @Title 编辑配置
// @Description 编辑配置
// @Param	request	body system.SysConfigEditReq true "type json string"
// @Success 200
// @router /config/edit [post]
func (this *SysConfigController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req system.SysConfigEditReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ConfigKey == "" {
		br.Msg = "参数错误"
		return
	}

	sysConfigObj := new(system.SysConfig)

	updateParams := make(map[string]interface{})
	updateParams["config_val"] = req.ConfigVal
	updateParams["modify_time"] = time.Now()

	whereParam := make(map[string]interface{})
	whereParam["config_key"] = req.ConfigKey

	err = sysConfigObj.Update(updateParams, whereParam)
	if err != nil {
		br.Msg = "保存失败！"
		br.ErrMsg = "保存失败,Err:" + err.Error()
		return
	}

	br.Msg = "保存成功"
	br.Ret = 200
	return
}
